package com.gxa.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.commons.R;
import com.gxa.finance.mapper.BillMapper;
import com.gxa.finance.mapper.FinancePaymentMapper;
import com.gxa.finance.pojo.Bill;
import com.gxa.finance.pojo.FinancePayment;
import com.gxa.finance.service.FinancePaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Service
public class FinancePaymentServiceImpl implements FinancePaymentService {
    @Autowired
    private FinancePaymentMapper financePaymentMapper;
    @Autowired
    private BillMapper billMapper;
    @Override
    public R payPage(Integer currentPage, Integer pageSize) {
        Page<FinancePayment> page = new Page(currentPage,pageSize);
        Page<FinancePayment> financePaymentPage = financePaymentMapper.selectPage(page, null);

        R r = new R();
        try{
            if (financePaymentPage!=null){
                r.setCode(200);
                r.setMsg("查询成功");
                r.setData(financePaymentPage);
            }
        }catch(Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("失败");
        }

        return r;
    }

    @Override
    //人工判断预审核通过操作接口
    public R audit(FinancePayment financePayment) {
        QueryWrapper<FinancePayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("information",financePayment.getInformation());
//        通过审核
        financePayment.setState(1);
        int i = financePaymentMapper.update(financePayment,queryWrapper);
        R r = new R();
        if(i>0){
            r.setCode(200);
            r.setMsg("审核通过");
        }

        List<FinancePayment> payments = financePaymentMapper.selectList(queryWrapper);
        List<BigDecimal> list = new ArrayList<>();
        BigDecimal amount=new BigDecimal("0.00");
        for (FinancePayment f:payments
             ) {
             amount = f.getAmount();  //应支付
            BigDecimal noInformation = f.getNoInformation();   //抵扣金额
            BigDecimal pay = f.getPay();    //已支付
            System.out.println("应支付"+amount+"抵扣金额"+noInformation+"已支付"+pay);
            System.out.println("拿到额度"+noInformation.add(pay));
            list.add(noInformation.add(pay));

        }
        BigDecimal a=new BigDecimal("0.00");
        BigDecimal b=new BigDecimal("0.00");
        for (BigDecimal o : list) {
            a=a.add(o);
        }
        if (amount.subtract(a).compareTo(b)<=0){
            Bill bill = new Bill();
            bill.setOrderId(financePayment.getInformation());
                UpdateWrapper<Bill> billQueryWrapper = new UpdateWrapper<>();
                billQueryWrapper.eq("order_id",bill.getOrderId());
            System.out.println("+++++++++++++++"+bill.getOrderId());
                bill.setbStatus(2);
                int update = billMapper.update(bill, billQueryWrapper);
        }

        System.out.println(amount.subtract(a));


        return r;
    }

    @Override
    // 人工判断预审核未通过操作接口
    public R noAudit(FinancePayment financePayment) {
        QueryWrapper<FinancePayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("information",financePayment.getInformation());
        //不通过审核
        financePayment.setState(2);
        int update = financePaymentMapper.update(financePayment, queryWrapper);
        R r = new R();
        if (update>0){
            r.setCode(200);
            r.setMsg(",审核不通过，重新抵扣");
        }
        return r;
    }


}
